package app.com.freshmanapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import app.com.freshmanapp.pojo.User;

public class RegistActivity2 extends AppCompatActivity implements View.OnClickListener {
    EditText et_name,et_password,et_email,et_phone,et_repassword;
    Button et_btn;
    Spinner sp_bumen,sp_zhuanye;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist2);
        init();
        et_btn.setOnClickListener(this);
    }

    private void init() {
        et_name=(EditText) findViewById(R.id.name);
        et_password=(EditText)findViewById(R.id.password);
        et_repassword=(EditText)findViewById(R.id.repassword);
        et_email=(EditText)findViewById(R.id.email);
        et_phone=(EditText)findViewById(R.id.phone);
        et_btn=(Button)findViewById(R.id.btn1);
        sp_bumen=(Spinner)findViewById(R.id.bumen);
        sp_zhuanye=(Spinner)findViewById(R.id.zhuanye);

    }

    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String password = et_password.getText().toString();
        String email = et_email.getText().toString();
        String phone = et_phone.getText().toString();

        User u=new User(name,password,phone,email,"学生");

        Gson gson=new Gson();
        String json = gson.toJson(u);
        System.out.println(json);
        String url="http://10.2.64.50:8080/app1/user/login";
        RequestQueue requestQueue = Volley.newRequestQueue(RegistActivity2.this);
        Request request= new JsonObjectRequest(Request.Method.POST, url, json, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject jsonObject) {
                try{
                    boolean success = jsonObject.getBoolean("success");

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("响应失败");
            }
        });
        requestQueue.add(request);

    }


}